<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\DepartmentModel;
use cmf\controller\AdminBaseController;

class DepartmentController extends AdminBaseController
{

    /**
     * 部门管理
     * @adminMenu(
     *     'name'   => '部门管理',
     *     'parent' => '',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 40,
     *     'icon'   => '',
     *     'remark' => '部门管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $model = new DepartmentModel();
        $list         = $model->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加部门
     * @adminMenu(
     *     'name'   => '添加部门',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加部门',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加部门提交
     * @adminMenu(
     *     'name'   => '添加部门提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加部门提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data           = $this->request->param();
        $model = new DepartmentModel();
        $result         = $model->validate(true)->save($data);
        if ($result === false) {
            $this->error($model->getError());
        }
        $this->success("添加成功！", url("index"));
    }

    /**
     * 编辑部门
     * @adminMenu(
     *     'name'   => '编辑部门',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑部门',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id             = $this->request->param('id');
        $model = new DepartmentModel();
        $result         = $model->where('id', $id)->find();
        $this->assign('info', $result);
        return $this->fetch();
    }

    /**
     * 编辑部门提交
     * @adminMenu(
     *     'name'   => '编辑部门提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑部门提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data           = $this->request->param();
        $model = new DepartmentModel();
        $result         = $model->validate(true)->save($data, ['id' => $data['id']]);
        if ($result === false) {
            $this->error($model->getError());
        }
        $this->success("保存成功！", url("index"));
    }

    /**
     * 删除部门
     * @adminMenu(
     *     'name'   => '删除部门',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除部门',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id             = $this->request->param('id', 0, 'intval');
        $model = new DepartmentModel();
        $result = $model->where(['id' => $id])->delete();
        $this->success("删除成功！", url("index"));
    }
}